package com.tm.d12_arrays;

import java.util.Arrays;
import java.util.function.IntToDoubleFunction;

/**
 * 2024/5/25 Cjj 掌握 Arrays类的常用方法 9:30
 */
public class ArraysTest1 {
    public static void main(String[] args) {
        //1.public static String toString(类型[] arr):返回数组的内容
        int[] arr={10,20,30,40,50,60};
        System.out.println(Arrays.toString(arr));

        //2.public static int[] copyOfRange(类型[] arr,起始索引,结束索引)：拷贝数组(指定范围,包前不包后)
        int[] arr2 = Arrays.copyOfRange(arr, 1, 4);
        System.out.println(Arrays.toString(arr2));

        //3.public static copyOf(类型[] arr, int newLength):拷贝数组, 可以指定新数组的长度
        int[] arr3 = Arrays.copyOf(arr, 5);
        System.out.println(Arrays.toString(arr3));

        int [] arr4={22,33,44,55,66};
        int[] arr5 = Arrays.copyOf(arr4, 5);
        System.out.println(Arrays.toString(arr5));

        //4.public static setAll(double[] array, IntToDoubleFunction generator):把数组中的原来数据改为新数据又存进去
         double[] price={99.8,128,100};

         //把每个价格都*0.8折
         Arrays.setAll(price, new IntToDoubleFunction() {
             @Override
             public double applyAsDouble(int value) {

                 return price[value]*0.8;
             }

         });
         Arrays.setAll(price,value -> price[value]*0.8);
         System.out.println(Arrays.toString(price));

        //5.public static void sort(类型[] arr):对数组进行排序(默认是升序排序)
        Arrays.sort(price);
        System.out.println(Arrays.toString(price));


    }
}
